package scoping.blocks;

import java.util.List;

import scoping.helpers.FaultyType.FaultType;
import scoping.helpers.ScopeException;
import ic.ast.decl.DeclMethod;
import ic.ast.decl.Parameter;
import ic.ast.stmt.LocalVariable;

public class MethodScope extends ScopeBlock {
	private List<Parameter> _params;
	private List<LocalVariable> _localVars;
	private DeclMethod _declMethod;
	
	public MethodScope(DeclMethod method, List<Parameter> params, List<LocalVariable> variables) throws ScopeException{
		_params = params;
		_localVars = variables;
		_declMethod = method;
		populateVariables(variables);
		populateParams(params);
		
	}
	
	public List<Parameter> getParams(){
		return _params;
	}
	public List<LocalVariable> getLocalVars(){
		return _localVars;
	}
	
	private void populateVariables(List<LocalVariable> vars) throws ScopeException
	{
		for (LocalVariable var : vars)
		{
			if (! TestAndSet(_variables, var.getName(), var.getType()))
			{
				throw new ScopeException(var.getLine(),String.format(FaultType.DEFINED.getDescription(), var.getName()));
			}
		}
	}
	
	private void populateParams(List<Parameter> params)
	{
		for (Parameter param : params)
		{
			_variables.put(param.getName(), param.getType());
		}
	}
	
	public DeclMethod getMethodDeclaration(){
		return _declMethod;
	}
}
